import re
from datetime import datetime , timedelta

from django.contrib.auth import get_user_model
from rest_framework.validators import UniqueValidator

from macroDjango.settings import REGEX_MOBILE
from .models import UserProfile, VerifyCode
from  rest_framework import serializers

User = get_user_model()


class UserRegSerializer(serializers.ModelSerializer):
    '''
        用户信息注册
    '''
    code = serializers.CharField(required=True , write_only=True , max_length=4 , min_length=4 , label="验证码" ,
                                 error_messages={
                                     "blank": "请输入验证码" ,
                                     "required": "请输入验证码" ,
                                     "max_length": "验证码格式错误" ,
                                     "min_length": "验证码格式错误"
                                 } ,
                                 help_text="验证码")
    username = serializers.CharField(label="用户名" , help_text="用户名" , required=True , allow_blank=False ,
                                     validators=[ UniqueValidator(queryset=User.objects.all() , message="用户已经存在") ])

    password = serializers.CharField(
        style={'input_type': 'password'} , help_text="密码" , label="密码" , write_only=True ,
    )

    # def create(self, validated_data):
    #     user = super(UserRegSerializer, self).create(validated_data=validated_data)
    #     user.set_password(validated_data["password"])
    #     user.save()
    #     return user

    def validate_code(self , code):
        # try:
        #     verify_records = VerifyCode.objects.get(mobile=self.initial_data["username"], code=code)
        # except VerifyCode.DoesNotExist as e:
        #     pass
        # except VerifyCode.MultipleObjectsReturned as e:
        #     pass
        verify_records = VerifyCode.objects.filter(mobile=self.initial_data[ "username" ]).order_by("-add_time")
        if verify_records:
            last_record = verify_records[ 0 ]

            five_mintes_ago = datetime.now() - timedelta(hours=0 , minutes=5 , seconds=0)
            if five_mintes_ago > last_record.add_time:
                raise serializers.ValidationError("验证码过期")

            if last_record.code != code:
                raise serializers.ValidationError("验证码错误")

        else:
            raise serializers.ValidationError("验证码错误")

    def validate(self , attrs):
        attrs[ "mobile" ] = attrs[ "username" ]
        del attrs[ "code" ]
        return attrs

    class Meta:
        model = User
        fields = ("username" , "code", "email", "password")


class VerifyCodeSerializers(serializers.Serializer):
    mobile = serializers.CharField(max_length=11,min_length=11)

    def validated_mobile(self,mobile):

        """
        验证手机号码
        :param data:
        :return: 手机验证码
        """
        # 手机是否注册
        if User.objects.filter(mobile=mobile).Count():
            raise serializers.ValidationError("手机号码已被注册")

        # 验证手机号码
        if not re.match(mobile,REGEX_MOBILE):
            raise serializers.ValidationError("手机号码格式不对")

        # 验证频率
        one_mintes_age= datetime.now() - timedelta(hours=0,minutes=1,seconds=0)
        if VerifyCode.objects.filter(add_time__gt=one_mintes_age,mobile=mobile):
            raise serializers.ValidationError("太频繁了，时间没有超过60秒")
        return mobile
